package zack.project.infrastructure.gateway;

import zack.project.trigger.api.ILotteryService;
import zack.project.trigger.api.dto.LotteryBaseRequestDTO;
import zack.project.trigger.api.dto.LotteryResponseDTO;
import zack.project.trigger.api.request.Request;
import zack.project.trigger.api.response.Response;
import zack.project.types.enums.ResponseCode;
import zack.project.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author A1793
 */
@Service
@Slf4j
public class LotteryRpcService {

    @Reference(version = "1.0",
            check = false,
            mock = "return \" RemoteService:lottery not available right now.\"")
    zack.project.trigger.api.ILotteryService lotteryService;

    public String lottery(Integer takeCount, Long strategyId) {
       try{
           LotteryBaseRequestDTO lotteryBaseRequestDTO = new LotteryBaseRequestDTO();
           Map<String, Object> extParams = new HashMap<String, Object>();
           extParams.put("takeCount", takeCount);
           lotteryBaseRequestDTO.setStrategyId(strategyId);
           lotteryBaseRequestDTO.setExtParams(extParams);
           lotteryBaseRequestDTO.setBizType("lUCKRATE");
           Request<LotteryBaseRequestDTO> request = new Request<>("group", "1", lotteryBaseRequestDTO);
           Response<LotteryResponseDTO> response = lotteryService.lottery(request);
           LotteryResponseDTO data = response.getData();
           log.info("营销抽奖接口调用成功 随机概率:{}",data.getAwardResult());
           return data.getAwardResult();
       }catch (Exception e){
           log.error("抽奖接口调用失败 错误信息:{} ", e.getMessage());
           throw new AppException(ResponseCode.RPC_EXCEPTION);
       }
    }


}
